<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script src="./react/react.development.js"></script>
  <script src="./react/react-dom.development.js"></script>
  <script src="./react/babel.min.js"></script>

  <div id="app"></div>

  <script type="text/babel">
    class App extends React.Component {
      constructor(props){
        super(props)
        this.state = {
          message: 'lwt'
        }
      }
      render(){
        return (
          <div>
            {/* 方案一：bind绑定this(显示绑定) */}
            <button onClick={this.btnClick1.bind(this)}>按钮</button>
            {/* 方案二：定义函数时, 使用箭头函数 */}
            <button onClick={this.btnClick2}>按钮2</button>
            {/* 方案三(推荐): 直接传入一个箭头函数, 在箭头函数中调用需要执行的函数： */}
            <button onClick={() => {this.btnClick3()}}>按钮</button>
          </div>
        )
      }
      btnClick1(){
        console.log(this.state.message)
      }
      // 箭头函数中永远不绑定this
      // ES6中给对象增加属性: class fields
      btnClick2 = () => {
        console.log(this.state.message)
      }
      btnClick3(){
        console.log(this.state.message)
      }
    }

    ReactDOM.render(<App/>, document.getElementById('app'))
  </script>

</body>

</html>